package com.retailshop.shopcontrol.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.retailshop.shopcontrol.common.HttpPost;
import com.retailshop.shopcontrol.entity.HttpRequestConfig;
import com.retailshop.shopcontrol.entity.PostJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@Controller
public class FinaceController {

    /**
     * 积分模块
     * 创建日期:2018年4月6日
     * 功能描述:通过request的方式来获取到json数据
     *
     * @param jsonobject 这个是阿里的 fastjson对象
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/finacejson", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String GetFinaceJson(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        try {
            // 直接将json信息打印出来
            System.out.println(jsonParam.toJSONString());

            //将接收到的数据转化为对象
            PostJson postJson = JSON.parseObject(jsonParam.toJSONString(), new TypeReference<PostJson>() {
            });
            String url = postJson.getUrl();
            //请求参数
            String parm = postJson.getParm();
            if (url.isEmpty() || parm.isEmpty()) {
                return "{\"code\":999999,\"msg\":\"参数错误\",\"errorDetail\":\"参数错误\",\"count\":0,\"data\":[]}";
            } else {
                int AID = -2;
                Object objAID = request.getSession().getAttribute("AID");
                if (objAID != null) {
                    AID = (int) objAID;
                }
                if (parm.length() > 1) {
                    parm = parm.replace("}", ",\"AID\":" + AID + "}");
                }
                return GetFinaceMod(request, url, parm);
            }
        } catch (Exception ex) {
            return "{\"code\":999999,\"msg\":\"访问错误\",\"errorDetail\":\"" + ex.getMessage() + "\",\"count\":0,\"data\":[]}";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/finace")
    public String GetFinace(HttpServletRequest request) {
        try {

            //请求地址
            String url=request.getParameter("url");
            //请求参数
            String parm="";

            //读取所有参数
            Enumeration paramNames = request.getParameterNames();
            while (paramNames.hasMoreElements()) {
                String paramName = (String) paramNames.nextElement();
                if(!paramName.equals("url"))
                {
                    parm +=",\""+paramName+"\":\""+request.getParameter(paramName)+"\"";
                }
            }

            if(url.isEmpty() || url=="")
            {
                return "{\"code\":999999,\"msg\":\"参数错误\",\"errorDetail\":\"参数错误\",\"count\":0,\"data\":[]}";
            }
            else
            {
                if(parm.length()>1)
                {
                    parm = parm.substring(1);

                    int AID =-2;
                    Object objAID = request.getSession().getAttribute("AID");
                    if(objAID!=null)
                    {
                        AID = (int)objAID;
                    }

                    parm +=",\"AID\":"+AID;

                }
                parm ="{"+parm+"}";
                return GetFinaceMod(request,url,parm);
            }
        } catch (Exception ex) {
            return "{\"code\":999999,\"msg\":\"访问错误\",\"errorDetail\":\""+ex.getMessage()+"\",\"count\":0,\"data\":[]}";
        }
    }

    public String GetFinaceMod(HttpServletRequest request, String url, String parm) {
        //模块URL
        String modUrl = HttpRequestConfig.finaceServerUrl;
        //模块密钥
        String modKey = "63cca9daa815b390f62f35f92e4d5392";

        return HttpPost.GetMod(request,modUrl,modKey,url,parm);
    }

}
